package cn.edu.xjtu.nbrash.xbarsuite.zxingdemo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.xjtu.nbrash.xbarsuite.R;

public class RashResultDesplayActivity extends Activity {

	private static final String TAG = RashResultDesplayActivity.class.getName();

	/**
	 * for the FloatButtonService to return the result
	 */
	public static final String ACTION_RESULT = "cn.edu.xjtu.nbrash.xbarsuite.RESULT";

	/**
	 * for the FloatButtonService decoder failed
	 */
	public static final String ACTION_FAILED = "cn.edu.xjtu.nbrash.xbarsuite.FAILED";

	/**
	 * all done action
	 */
	public static final String ACTION_DONE = "cn.edu.xjtu.nbrash.xbarsuite.DONE";

	/**
	 * for the FloatButtonService to return the bitmap to decode
	 */
	public static final String ACTION_BITMAP = "cn.edu.xjtu.nbrash.xbarsuite.BITMAP";

	/**
	 * extra parameter bitmap
	 */
	public static final String EXTRA_BITMAP = "BITMAP";

	/**
	 * extra parameter result
	 */
	public static final String EXTRA_RESULT = "RESULT";

	// just for temporary
	private TextView rashResultDesplay;

	private ImageView rashImageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rash_result_desplay);

		rashResultDesplay = (TextView) findViewById(R.id.rashResultDisplay);
		rashImageView = (ImageView) findViewById(R.id.imgVcropBar);

	}

	@Override
	protected void onResume() {
		Intent intent = getIntent();

		if (ACTION_RESULT.equals(intent.getAction())) {
			String result = intent.getStringExtra(EXTRA_RESULT);
			if (result == null) {
				Toast.makeText(this, "Decoder Failed!", Toast.LENGTH_LONG)
						.show();
				finish();
			} else {
				rashResultDesplay.setText(result);
			}
			Uri crop = intent.getData();
			if (crop != null) {
				rashImageView.setImageURI(crop);
			} else {
				Log.e(TAG, "Null result image!");
			}
			intent.setAction(ACTION_DONE);
		}
		super.onResume();
	}

}
